/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package carreras.emuard.test;

import carreras.logger.CarrerasLogger;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *
 * @author Fanky10 <fanky10@gmail.com>
 * es un tcp que sirve para enviarle comandos al servidor (:
 */
public class ArduinoTCPClient {
    private boolean sendCommand(byte[] bytes) {
        boolean returnValueOnError = false;
        try {
            Socket clientSocket = new Socket(InetAddress.getLocalHost(), ArduinoTCPServer.SINGLE_INSTANCE_NETWORK_SOCKET_PORT);
            OutputStream out = clientSocket.getOutputStream();
            out.write(bytes);
            out.close();
            clientSocket.close();
            CarrerasLogger.warn(ArduinoTCPClient.class, "Successfully notified first instance.");
            return true;
        } catch (UnknownHostException e) {
            CarrerasLogger.warn(ArduinoTCPClient.class, e.getMessage() + e);
            return returnValueOnError;
        } catch (IOException e1) {
            CarrerasLogger.warn(ArduinoTCPClient.class, "Error connecting to local port for single instance notification");
            CarrerasLogger.warn(ArduinoTCPClient.class, e1.getMessage() + e1);
            return returnValueOnError;
        }
    }
    public boolean sendCommand(int nro_carril){
        if(nro_carril<1 || nro_carril>2){
            throw new IllegalArgumentException("carril not supported!");
        }
        if(nro_carril == 1){
            return sendCommand(ArduinoTCPServer.COMANDO_CARRIL1.getBytes());
        }else if(nro_carril == 2){
            return sendCommand(ArduinoTCPServer.COMANDO_CARRIL2.getBytes());
        }else{
            throw new IllegalArgumentException("carril not supported!");
        }
        
    }
    public static void main(String args[]){
        new ArduinoTCPClient().sendCommand(1);
        new ArduinoTCPClient().sendCommand(2);
    }
}
